﻿using System.Net.Sockets;
using System.Net;

using BodaNetCommunications.Utilities.Results;

namespace BodaNetCommunications.Cores
{
    /// <summary>
    /// 通信单元
    /// </summary>
    public interface ICommUnit
    {
        /// <summary>
        /// 连接超时时间
        /// </summary>
        int ConnectTimeout { get; set; }
        /// <summary>
        /// 打开动作
        /// </summary>
        /// <param name="timeout"></param>
        /// <returns></returns>
        BodaResult<bool> Open(int timeout);
        /// <summary>
        /// 关闭连接
        /// </summary>
        void Close();
        /// <summary>
        /// 发送与接收报文
        /// </summary>
        BodaResult<byte> SendAndReceive(List<byte> req, int receiveLen = 1024, int errorLen = 0);

        /// <summary>
        /// 异步发送和接收报文
        /// </summary>
        /// <param name="reqBytes"></param>
        /// <param name="callback">接收报文的回调</param>
        void SendAsync(List<byte> reqBytes, Action<BodaResult<byte>> callback);

        BodaResult SendMsgc(byte[] bytes, int timeout = 1500);

        BodaResult<byte> ReceiveMsg(int timeout = 2000, int bytesLen = 1024);
        bool IsConnected { get; }

        EndPoint? LocalEndPoint { get; }
    }
}
